#!/bin/bash
#docker stop DeepSeek-R1-Distill-Llama-8B  && docker rm  DeepSeek-R1-Distill-Llama-8B
File_ip="168.17.100.17"
File_password="Huawei@123"
host_ip=host_ip=$(ip a show bond4 | grep -oP 'inet \K[\d.]+')
echo $host_ip
echo "加载镜像"
image_tag=mindie-2.0.RC2-300I-Duo-py311-openeuler24.03-lts.tar
if ! docker images -q|grep -q "1f7db5984eaf" ;then
    echo "2.0.RC2-300I-Duo-py311-openeuler24.03-lts镜像不存在"
    if ! which sshpass > /dev/null 2>&1; then
       echo "sshpass命令不存在，安装sshpass"
       yum install sshpass  -y
    fi
    echo "配置免密"
    ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa -q <<<y >/dev/null 2>&1

    if ! sshpass -p $File_password  ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@$File_ip  >/dev/null; then
        echo "错误: mindieservice_daemon 未成功启动"
        exit 1
    fi

    #sshpass -p $File_password  ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@$File_ip
    echo "拷贝镜像tar"
    scp -r /home/data_mnt/wubian_data/Images/MindIE/$image_tag /home
    echo "加载镜像"
    docker load -i /home/$image_tag
fi

echo "检查权重是否存在"
model_weight_path="/home/DeepSeek-R1-Distill-Llama-8B"
if [ -e $model_weight_path ]; then
    echo "文件存在"
else
   scp -r /home/data_mnt/STM_Lab_Image_And_Data/DeepSeek_Lab/DeepSeek-R1-Distill-Llama-8B-310P-DUO/models/DeepSeek-R1-Distill-Llama-8B /home
fi


CONTAINER="DeepSeek-R1-Distill-Llama-8B"
docker_image="swr.cn-south-1.myhuaweicloud.com/ascendhub/mindie:2.0.RC2-300I-Duo-py311-openeuler24.03-lts"
docker run -itd --name $CONTAINER --ipc=host --net=host   \
--device=/dev/davinci0 \
--device=/dev/davinci1 \
--device=/dev/davinci2 \
--device=/dev/davinci3 \
--device=/dev/davinci4 \
--device=/dev/davinci5 \
--device=/dev/davinci6 \
--device=/dev/davinci7 \
--device=/dev/davinci_manager \
--device=/dev/devmm_svm \
--device=/dev/hisi_hdc \
-v /usr/local/dcmi:/usr/local/dcmi \
-v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
-v /usr/local/Ascend/driver/lib64/common:/usr/local/Ascend/driver/lib64/common \
-v /usr/local/Ascend/driver/lib64/driver:/usr/local/Ascend/driver/lib64/driver \
-v /etc/ascend_install.info:/etc/ascend_install.info \
-v /etc/vnpu.cfg:/etc/vnpu.cfg \
-v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \
-v $model_weight_path:$model_weight_path \
$docker_image \
 /bin/bash



# 检查容器是否运行
if ! docker ps -q -f name="$CONTAINER" | grep -q .; then
    echo "错误: 容器 '$CONTAINER' 未运行"
    exit 1
fi


cat <<EOF >  /home/config.json

{
    "Version" : "1.0.0",

    "ServerConfig" :
    {
        "ipAddress" : "$host_ip",
        "managementIpAddress" : "127.0.0.2",
        "port" : 1025,
        "managementPort" : 1026,
        "metricsPort" : 1027,
        "allowAllZeroIpListening" : false,
        "maxLinkNum" : 1000,
        "httpsEnabled" : false,
        "fullTextEnabled" : false,
        "tlsCaPath" : "security/ca/",
        "tlsCaFile" : ["ca.pem"],
        "tlsCert" : "security/certs/server.pem",
        "tlsPk" : "security/keys/server.key.pem",
        "tlsPkPwd" : "security/pass/key_pwd.txt",
        "tlsCrlPath" : "security/certs/",
        "tlsCrlFiles" : ["server_crl.pem"],
        "managementTlsCaFile" : ["management_ca.pem"],
        "managementTlsCert" : "security/certs/management/server.pem",
        "managementTlsPk" : "security/keys/management/server.key.pem",
        "managementTlsPkPwd" : "security/pass/management/key_pwd.txt",
        "managementTlsCrlPath" : "security/management/certs/",
        "managementTlsCrlFiles" : ["server_crl.pem"],
        "kmcKsfMaster" : "tools/pmt/master/ksfa",
        "kmcKsfStandby" : "tools/pmt/standby/ksfb",
        "inferMode" : "standard",
        "interCommTLSEnabled" : true,
        "interCommPort" : 1121,
        "interCommTlsCaPath" : "security/grpc/ca/",
        "interCommTlsCaFiles" : ["ca.pem"],
        "interCommTlsCert" : "security/grpc/certs/server.pem",
        "interCommPk" : "security/grpc/keys/server.key.pem",
        "interCommPkPwd" : "security/grpc/pass/key_pwd.txt",
        "interCommTlsCrlPath" : "security/grpc/certs/",
        "interCommTlsCrlFiles" : ["server_crl.pem"],
        "openAiSupport" : "vllm",
        "tokenTimeout" : 600,
        "e2eTimeout" : 600,
        "distDPServerEnabled":false
    },

    "BackendConfig" : {
        "backendName" : "mindieservice_llm_engine",
        "modelInstanceNumber" : 1,
        "npuDeviceIds" : [[0,1,2,3]],
        "tokenizerProcessNumber" : 8,
        "multiNodesInferEnabled" : false,
        "multiNodesInferPort" : 1120,
        "interNodeTLSEnabled" : true,
        "interNodeTlsCaPath" : "security/grpc/ca/",
        "interNodeTlsCaFiles" : ["ca.pem"],
        "interNodeTlsCert" : "security/grpc/certs/server.pem",
        "interNodeTlsPk" : "security/grpc/keys/server.key.pem",
        "interNodeTlsPkPwd" : "security/grpc/pass/mindie_server_key_pwd.txt",
        "interNodeTlsCrlPath" : "security/grpc/certs/",
        "interNodeTlsCrlFiles" : ["server_crl.pem"],
        "interNodeKmcKsfMaster" : "tools/pmt/master/ksfa",
        "interNodeKmcKsfStandby" : "tools/pmt/standby/ksfb",
        "ModelDeployConfig" :
        {
            "maxSeqLen" : 2560,
            "maxInputTokenLen" : 2048,
            "truncation" : false,
            "ModelConfig" : [
                {
                    "modelInstanceType" : "Standard",
                    "modelName" : "llama_65b",
                    "modelWeightPath" : "$model_weight_path",
                    "worldSize" : 4,
                    "cpuMemSize" : 5,
                    "npuMemSize" : -1,
                    "backendType" : "atb",
                    "trustRemoteCode" : false
                }
            ]
        },

        "ScheduleConfig" :
        {
            "templateType" : "Standard",
            "templateName" : "Standard_LLM",
            "cacheBlockSize" : 128,

            "maxPrefillBatchSize" : 50,
            "maxPrefillTokens" : 8192,
            "prefillTimeMsPerReq" : 150,
            "prefillPolicyType" : 0,

            "decodeTimeMsPerReq" : 50,
            "decodePolicyType" : 0,

            "maxBatchSize" : 200,
            "maxIterTimes" : 512,
            "maxPreemptCount" : 0,
            "supportSelectBatch" : false,
            "maxQueueDelayMicroseconds" : 5000
        }
    }
}

EOF


docker cp  /home/config.json  $CONTAINER:/usr/local/Ascend/mindie/latest/mindie-service/conf/config.json
echo "在容器内执行命令序列"
docker exec -i "$CONTAINER" bash <<'EOF'
set -e  # 命令失败时立即退出

chmod -R 640 /usr/local/Ascend/mindie/latest/mindie-service/conf/config.json
# 启动服务（后台运行并保存日志）
cd /usr/local/Ascend/mindie/latest/mindie-service/
nohup ./bin/mindieservice_daemon >"$model_weight_path/1.log" 2>&1 &

# 可选：等待服务启动（根据实际情况调整）
sleep 5

# 验证服务是否运行
if ! pgrep -f mindieservice_daemon >/dev/null; then
    echo "错误: mindieservice_daemon 未成功启动"
    exit 1
fi

echo "服务已成功启动"
EOF

# 可选：检查日志文件
echo "查看服务日志: docker exec -it $CONTAINER tail -f $model_weight_path/1.log"
sleep 5
echo $model_weight_path
tail -f $model_weight_path/1.log



